<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chat Interface</title>
    <style>
        #chat-container {
            max-width: 600px;
            margin: 20px auto;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            box-sizing: border-box;
        }

        #chat {
            overflow-y: scroll;
            max-height: 300px;
            margin-bottom: 10px;
        }

        #user-input {
            width: 70%;
            padding: 10px;
            box-sizing: border-box;
        }

        #send-button {
            padding: 10px;
            cursor: pointer;
            background-color: #4caf50;
            color: #fff;
            border: none;
            border-radius: 4px;
        }

        #send-button:hover {
            background-color: #45a049;
        }

        .message {
            padding: 8px;
            margin-bottom: 8px;
            border-radius: 8px;
            display: inline-block;
            max-width: 70%; /* Adjust this value as needed */
        }

        .user {
            color: #007bff;
            text-align: right;
            background-color: #e6e6e6; /* Adjust the background color as needed */
        }

        .chat {
            color: #28a745;
            text-align: left;
            background-color: #d3f8d3; /* Adjust the background color as needed */
        }


    </style>
</head>
<body>
    <div id="chat-container">
        <div id="chat"></div>
        <input type="text" id="user-input" placeholder="Type your message..." onkeydown="handleKeyPress(event)">
        <button id="send-button" onclick="sendMessage()" >Send</button>
    </div>

    <script>
        const chatApiEndpoint = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro';
        // 去 https://developer.baidu.com/article/detail.html?id=1089328 查看如和获取access_token
        const accessToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        function handleKeyPress(event) {
            if (event.key === 'Enter') {
                sendMessage();
            }
        }

        async function sendMessage() {
            const userInput = document.getElementById('user-input').value;
            appendMessage('user', userInput);

            const urlParams = new URLSearchParams({
                access_token: accessToken,
                // Add other parameters...
            });

            const fullUrl = `${chatApiEndpoint}?${urlParams.toString()}`;

            try {
                const response = await fetch(fullUrl, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        messages: [
                            {
                                role: 'user',
                                content: userInput
                            }
                        ]
                    }),
                });

                if (!response.ok) {
                    throw new Error(`HTTP error! Status: ${response.status}`);
                }

                const data = await response.json();
                const chatResponse = data.result;
                appendMessage('chat', chatResponse);
            } catch (error) {
                console.error('Error sending message to Baidu:', error);
            }

            document.getElementById('user-input').value = '';
        }

        function appendMessage(sender, message) {
            const chatContainer = document.getElementById('chat');
            const messageElement = document.createElement('div');

            if (sender === 'user') {
                messageElement.classList.add('user', 'message');
            } else {
                messageElement.classList.add('chat', 'message');
            }

            messageElement.textContent = message;
            chatContainer.appendChild(messageElement);

            // Add a line break after each message
            chatContainer.appendChild(document.createElement('br'));
        }

    </script>
</body>
</html>
